//
//  GKDYVideoView.m
//  GKDYVideo
//
//  Created by QuintGao on 2018/9/23.
//  Copyright © 2018 QuintGao. All rights reserved.
//

#import "LYouVideoView.h"
#import "LYouVideoPlayer.h"

@interface LYouVideoView()<UIScrollViewDelegate, GKDYVideoPlayerDelegate, GKDYVideoControlViewDelegate>

@property (nonatomic, strong) UIScrollView              *scrollView;

// 创建三个控制视图，用于滑动切换
@property (nonatomic, strong) LYouVideoControlView      *topView;   // 顶部视图
@property (nonatomic, strong) LYouVideoControlView      *ctrView;   // 中间视图
@property (nonatomic, strong) LYouVideoControlView      *btmView;   // 底部视图

// 控制播放的索引，不完全等于当前播放内容的索引
@property (nonatomic, assign) NSInteger                 index;

// 当前播放内容是h索引
@property (nonatomic, assign) NSInteger                 currentPlayIndex;

@property (nonatomic, weak) UIViewController            *vc;
@property (nonatomic, assign) BOOL                      isPushed;

@property (nonatomic, strong) NSMutableArray            *videos;

@property (nonatomic, strong) LYouVideoPlayer           *player;

// 记录播放内容
@property (nonatomic, copy) NSString                    *currentPlayId;

// 记录滑动前的播放状态
@property (nonatomic, assign) BOOL                      isPlaying_beforeScroll;

@end

@implementation LYouVideoView

- (instancetype)initWithVC:(UIViewController *)vc isPushed:(BOOL)isPushed {
    if (self = [super init]) {
        self.vc = vc;
        self.isPushed = isPushed;

        [self addSubview:self.scrollView];
        [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        [LYouLoadingView show];
        if (!isPushed) {
            [self.viewModel refreshNewListWithSuccess:^(NSArray * _Nonnull list) {
                [LYouLoadingView hide];
                [self setModels:list index:0];
                /** 本地服务器校验 */
                NSURL *receiptURL = [[NSBundle mainBundle] appStoreReceiptURL];
                NSData *receiptData = [NSData dataWithContentsOfURL:receiptURL];
                NSString *encodeStr = [receiptData base64EncodedStringWithOptions:0];
                if (encodeStr.length==0) {
                    encodeStr = @"";
                }
                
                NSDictionary *parms = @{@"receipt-data":encodeStr,@"password":SHAREKEY};
                /** 校验是否购买会员 */
                [self requestAppStoreServer:parms];
            } failure:^(NSError * _Nonnull error) {
                NSLog(@"%@", error);
            }];
            
            self.scrollView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
                [self.videos removeAllObjects];
                [self.viewModel refreshNewListWithSuccess:^(NSArray * _Nonnull list) {
                    [self setModels:list index:0];
                    [self.scrollView.mj_header endRefreshing];
                } failure:^(NSError * _Nonnull error) {
                    NSLog(@"%@", error);
                    [self.scrollView.mj_header endRefreshing];
                }];
            }];
        }
    }
    return self;
}

#pragma mark - 服务器校验支付成功凭证
-(void)requestAppStoreServer:(NSDictionary *)parms{
    
    NSUserDefaults *isHaveBuy = [NSUserDefaults standardUserDefaults];
    NSString *url = [NSString stringWithFormat:@"%@/iosCheck",LYBASEURL];
    
    [[LYouNetClient sharedInstance]NetRequestPOSTWithRequestURL:url WithParameter:parms WithReturnValeuBlock:^(NSURLSessionDataTask *task, id  _Nullable responseObject, NSArray *responseDict) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        NSString *codeStr = [NSString stringWithFormat:@"%@",dic[@"status"]];
        
        if ([codeStr isEqualToString:@"0"]) {
            /** 服务器购买校验成功 */
            [isHaveBuy setValue:@"yes" forKey:@"ISHAVEBUYMEMBER"];
        }else{
            [isHaveBuy setValue:@"no" forKey:@"ISHAVEBUYMEMBER"];
        }
        /** 审核开关 */
        [self isCheckIOS];
    } WithErrorBlock:^(NSURLSessionDataTask *task, NSError *error) {
        
    } WithFailureBlock:^{
        
    }];
}

#pragma mark - 审核开关
-(void)isCheckIOS{
    NSString *url = [NSString stringWithFormat:@"%@/iosSwitch",LYBASEURL];
    [[LYouNetClient sharedInstance]NetRequestGETWithRequestURL:url WithParameter:nil WithReturnValeuBlock:^(NSURLSessionDataTask *task,id  _Nullable responseObject,NSArray *responseArray) {
        NSDictionary *resDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        NSString *isCheckIos = [NSString stringWithFormat:@"%@",resDic[@"status"]];
        NSUserDefaults *isCheck = [NSUserDefaults standardUserDefaults];
        if ([isCheckIos isEqualToString:@"yes"]) {
            [isCheck setValue:@"yes" forKey:@"CHECKIOS"];
        }else{
            [isCheck setValue:@"no" forKey:@"CHECKIOS"];
        }
        
        if (self.pushToBuyBlock) {
            self.pushToBuyBlock(ISCheckIOS);
        }
        
    } WithErrorBlock:^(NSURLSessionDataTask *task, NSError *error) {
        
    } WithFailureBlock:^{
        
    }];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGFloat controlW = CGRectGetWidth(self.scrollView.frame);
    CGFloat controlH = CGRectGetHeight(self.scrollView.frame);
    self.topView.frame   = CGRectMake(0, 0, controlW, controlH);
    self.ctrView.frame   = CGRectMake(0, controlH, controlW, controlH);
    self.btmView.frame   = CGRectMake(0, 2 * controlH, controlW, controlH);
    WeakSelf(weakSelf);
    self.topView.clickPlayButtonBlock = ^{
        [weakSelf.player resumePlay];
        weakSelf.topView.playBtn.hidden = YES;
    };
    self.ctrView.clickPlayButtonBlock = ^{
        [weakSelf.player resumePlay];
        weakSelf.ctrView.playBtn.hidden = YES;
    };
    self.btmView.clickPlayButtonBlock = ^{
        [weakSelf.player resumePlay];
        weakSelf.btmView.playBtn.hidden = YES;
    };
}

#pragma mark - Public Methods
#pragma mark - Public Methods
- (void)setModels:(NSArray *)models index:(NSInteger)index {
    [self.videos removeAllObjects];
    [self.videos addObjectsFromArray:models];
    
    self.index = index;
    self.currentPlayIndex = index;
    
    if (models.count == 0) return;
    
    if (models.count == 1) {
        [self.ctrView removeFromSuperview];
        [self.btmView removeFromSuperview];
        
        self.scrollView.contentSize = CGSizeMake(0, SCREEN_HEIGHT);
        
        self.topView.model = self.videos.firstObject;
    }else if (models.count == 2) {
        [self.btmView removeFromSuperview];
        
        self.scrollView.contentSize = CGSizeMake(0, SCREEN_HEIGHT * 2);
        
        self.topView.model = self.videos.firstObject;
        self.ctrView.model = self.videos.lastObject;
        
        if (index == 1) {
            self.scrollView.contentOffset = CGPointMake(0, SCREEN_HEIGHT);
        }
    }else {
        if (index == 0) {   // 如果是第一个，则显示上视图，且预加载中下视图
            self.topView.model = self.videos[index];
            self.ctrView.model = self.videos[index + 1];
            self.btmView.model = self.videos[index + 2];
            // 播放第一个
            [self playVideoFrom:self.topView];
        }else if (index == models.count - 1) { // 如果是最后一个，则显示最后视图，且预加载前两个
            self.btmView.model = self.videos[index];
            self.ctrView.model = self.videos[index - 1];
            self.topView.model = self.videos[index - 2];
            
            // 显示最后一个
            self.scrollView.contentOffset = CGPointMake(0, SCREEN_HEIGHT * 2);
            // 播放最后一个
            [self playVideoFrom:self.btmView];
        }else { // 显示中间，播放中间，预加载上下
            self.ctrView.model = self.videos[index];
            self.topView.model = self.videos[index - 1];
            self.btmView.model = self.videos[index + 1];
            
            // 显示中间
            self.scrollView.contentOffset = CGPointMake(0, SCREEN_HEIGHT);
            // 播放中间
            [self playVideoFrom:self.ctrView];
        }
    }
}

- (void)pause {
    if (self.player.isPlaying) {
        self.isPlaying_beforeScroll = YES;
    }else {
        self.isPlaying_beforeScroll = NO;
    }
    
    [self.player pausePlay];
}

- (void)resume {
    if (self.isPlaying_beforeScroll) {
        [self.player resumePlay];
    }
}

- (void)destoryPlayer {
    self.scrollView.delegate = nil;
    [self.player removeVideo];
}

#pragma mark - Private Methods
- (void)playVideoFrom:(LYouVideoControlView *)fromView {
    // 移除原来的播放
    [self.player removeVideo];
    
    // 取消原来视图的代理
    self.currentPlayView.delegate = nil;
    
    // 切换播放视图
    self.currentPlayId    = fromView.model.post_id;
    self.currentPlayView  = fromView;
    self.currentPlayIndex = [self indexOfModel:fromView.model];
    // 设置新视图的代理
    self.currentPlayView.delegate = self;
    
    NSLog(@"currentPlayId==========%@",self.currentPlayId);
    // 重新播放
    @weakify(self);
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        @strongify(self);
        [self.player playVideoWithView:fromView.coverImgView url:fromView.model.video_url];
    });
}

// 获取当前播放内容的索引
- (NSInteger)indexOfModel:(LYouVideoModel *)model {
    __block NSInteger index = 0;
    [self.videos enumerateObjectsUsingBlock:^(LYouVideoModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
//        NSLog(@"idx====%lu",(unsigned long)idx);
        if ([model.post_id isEqualToString:obj.post_id]) {
            index = idx;
        }
    }];
    return index;
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    // 小于等于三个，不用处理
    if (self.videos.count <= 3) return;
    
    // 上滑到第一个
    if (self.index == 0 && scrollView.contentOffset.y <= SCREEN_HEIGHT) {
        return;
    }
    // 下滑到最后一个
    if (self.index == self.videos.count - 1 && scrollView.contentOffset.y > SCREEN_HEIGHT) {
        return;
    }
    
    // 判断是从中间视图上滑还是下滑
    if (scrollView.contentOffset.y >= 2 * SCREEN_HEIGHT) {  // 上滑
        [self.player removeVideo];  // 在这里移除播放，解决闪动的bug
        if (self.index == 0) {
            self.index += 2;
            
            scrollView.contentOffset = CGPointMake(0, SCREEN_HEIGHT);
            
            self.topView.model = self.ctrView.model;
            self.ctrView.model = self.btmView.model;
            
        }else {
            self.index += 1;
            
            if (self.index == self.videos.count - 1) {
                self.ctrView.model = self.videos[self.index - 1];
            }else {
                scrollView.contentOffset = CGPointMake(0, SCREEN_HEIGHT);
                
                self.topView.model = self.ctrView.model;
                self.ctrView.model = self.btmView.model;
            }
        }
        if (self.index < self.videos.count - 1) {
            self.btmView.model = self.videos[self.index + 1];
        }
    }else if (scrollView.contentOffset.y <= 0) { // 下滑
        [self.player removeVideo];  // 在这里移除播放，解决闪动的bug
        if (self.index == 1) {
            self.topView.model = self.videos[self.index - 1];
            self.ctrView.model = self.videos[self.index];
            self.btmView.model = self.videos[self.index + 1];
            self.index -= 1;
        }else {
            if (self.index == self.videos.count - 1) {
                self.index -= 2;
            }else {
                self.index -= 1;
            }
            scrollView.contentOffset = CGPointMake(0, SCREEN_HEIGHT);
            
            self.btmView.model = self.ctrView.model;
            self.ctrView.model = self.topView.model;
            
            if (self.index > 0) {
                self.topView.model = self.videos[self.index - 1];
            }
        }
    }
}

// 结束滚动后开始播放
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSLog(@"over=========%ld",(long)self.currentPlayIndex);
    if (scrollView.contentOffset.y == 0) {
        if (self.currentPlayId == self.topView.model.post_id) return;
        [self playVideoFrom:self.topView];
    }else if (scrollView.contentOffset.y == SCREEN_HEIGHT) {
        if (self.currentPlayId == self.ctrView.model.post_id) return;
        [self playVideoFrom:self.ctrView];
    }else if (scrollView.contentOffset.y == 2 * SCREEN_HEIGHT) {
        if (self.currentPlayId == self.btmView.model.post_id) return;
        [self playVideoFrom:self.btmView];
    }
    
    if (self.isPushed) return;
    // 当只剩最后两个的时候，获取新数据
    if (self.currentPlayIndex == self.videos.count - 2) {
        [self.viewModel refreshNewListWithSuccess:^(NSArray * _Nonnull list) {
            [self.videos addObjectsFromArray:list];
        } failure:^(NSError * _Nonnull error) {
            NSLog(@"%@", error);
        }];
    }
    if (self.currentPlayIndex%5==0) {
        /** 插播广告 */
        if ([ISHAVEBUYMEMBER isEqualToString:@"no"]) {
            /** 插播广告 */
            if (self.showAdboadBlock) {
                self.showAdboadBlock();
            }
        }
    }
}

#pragma mark - GKDYVideoPlayerDelegate
- (void)player:(LYouVideoPlayer *)player statusChanged:(GKDYVideoPlayerStatus)status {
    switch (status) {
        case GKDYVideoPlayerStatusUnload:   // 未加载
            
            break;
        case GKDYVideoPlayerStatusPrepared:   // 准备播放
            
            break;
        case GKDYVideoPlayerStatusLoading: {     // 加载中
            [self.currentPlayView startLoading];
            [self.currentPlayView hidePlayBtn];
        }
            break;
        case GKDYVideoPlayerStatusPlaying: {    // 播放中
            [self.currentPlayView stopLoading];
            [self.currentPlayView hidePlayBtn];
        }
            break;
        case GKDYVideoPlayerStatusPaused: {     // 暂停
            [self.currentPlayView stopLoading];
            [self.currentPlayView showPlayBtn];
        }
            break;
        case GKDYVideoPlayerStatusEnded: {   // 播放结束
            // 重新开始播放
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                if ([ISAUTOPLAY isEqualToString:@"yes"]) {
                    [self.player resetPlay];
                }else{
                    [self settingAutoPaly];
                }
            });
        }
            break;
        case GKDYVideoPlayerStatusError:   // 错误
            break;
        default:
            break;
    }
}

-(void)settingAutoPaly{
    //设置偏移量
    LYouVideoModel *firstModel = self.videos[0];
    if ([firstModel.post_id isEqualToString:self.currentPlayView.model.post_id]) {
        [self.scrollView setContentOffset:CGPointMake(0,SCREEN_HEIGHT) animated:YES];
    }else{
        [self.scrollView setContentOffset:CGPointMake(0,SCREEN_HEIGHT*2) animated:YES];
    }
    self.currentPlayId = self.currentPlayView.model.post_id;
    [self playVideoFrom:self.ctrView];
//    [self.player resumePlay];
    // 当只剩最后两个的时候，获取新数据
    if (self.currentPlayIndex == self.videos.count - 2) {
        [self.viewModel refreshNewListWithSuccess:^(NSArray * _Nonnull list) {
            NSLog(@"list========%@",list);
            [self.videos addObjectsFromArray:list];
        } failure:^(NSError * _Nonnull error) {
            NSLog(@"%@", error);
        }];
    }
    if (self.currentPlayIndex%5==0) {
        /** 插播广告 */
        if ([ISHAVEBUYMEMBER isEqualToString:@"no"]) {
            /** 插播广告 */
            if (self.showAdboadBlock) {
                self.showAdboadBlock();
            }
        }
    }
}

- (void)player:(LYouVideoPlayer *)player currentTime:(float)currentTime totalTime:(float)totalTime progress:(float)progress {
    dispatch_async(dispatch_get_main_queue(), ^{
//        [self.currentPlayView setProgress:progress];
    });
}

#pragma mark - GKDYVideoControlViewDelegate
- (void)controlViewDidClickSelf:(LYouVideoControlView *)controlView {
    if (self.player.isPlaying) {
        [self.player pausePlay];
    }else {
        [self.player resumePlay];
    }
}

- (void)controlViewDidClickFavorite:(LYouVideoControlView *)controlView {
    NSLog(@"收藏链接=========%@",controlView.model.video_url);
    controlView.favoriteBtn.selected = !controlView.favoriteBtn.selected;
    NSString *favor;
    if (controlView.favoriteBtn.selected==YES) {
        favor = @"1";
    }else{
        favor = @"2";
    }
    if (self.clickFavorClickBlock) {
        self.clickFavorClickBlock(controlView.model,favor);
    }
}

- (void)controlViewDidClickDetailBtn:(LYouVideoControlView *)controlView {
    NSLog(@"详情链接=========%@",controlView.model.video_url);
    if (self.detailClickBlock) {
        self.detailClickBlock(controlView.model);
    }
}

#pragma mark - 懒加载
- (LYouVideoViewModel *)viewModel {
    if (!_viewModel) {
        _viewModel = [LYouVideoViewModel new];
    }
    return _viewModel;
}

- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [UIScrollView new];
        _scrollView.pagingEnabled = YES;
        _scrollView.backgroundColor = [UIColor clearColor];
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.delegate = self;
        
        [_scrollView addSubview:self.topView];
        [_scrollView addSubview:self.ctrView];
        [_scrollView addSubview:self.btmView];
        _scrollView.contentSize = CGSizeMake(0, SCREEN_HEIGHT * 3);
        
        if (@available(iOS 11.0, *)) {
            _scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            // Fallback on earlier versions
        }
    }
    return _scrollView;
}

- (LYouVideoControlView *)topView {
    if (!_topView) {
        _topView = [LYouVideoControlView new];
    }
    return _topView;
}

- (LYouVideoControlView *)ctrView {
    if (!_ctrView) {
        _ctrView = [LYouVideoControlView new];
    }
    return _ctrView;
}

- (LYouVideoControlView *)btmView {
    if (!_btmView) {
        _btmView = [LYouVideoControlView new];
    }
    return _btmView;
}

- (NSMutableArray *)videos {
    if (!_videos) {
        _videos = [NSMutableArray new];
    }
    return _videos;
}

- (LYouVideoPlayer *)player {
    if (!_player) {
        _player = [LYouVideoPlayer new];
        _player.delegate = self;
    }
    return _player;
}

@end
